package aar.test.tessocr.adapter;

import android.content.Context;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import aar.test.tessocr.model.CompanySearchModel;
import aar.test.tessocr.utils.KeywordUtil;
import clx.tessocr.ocreng.R;

/**
 * Created by 149-201605 on 2016/8/1.
 */

/*
* 主界面显示 dialog的资源适配器
* */
public class CompanySearchInfoAdapter extends BaseAdapter {

    private List<CompanySearchModel> mList;

    private Context mContext;

    private String keyword;

    public CompanySearchInfoAdapter(Context mContext, List<CompanySearchModel> mList, String keyword) {
        this.mList = mList;
        this.mContext = mContext;
        this.keyword = keyword;

    }



    @Override
    public int getCount() {
        int count = 0;
        if (null != mList && !mList.isEmpty()) {
            count = mList.size();
        }
        return count;
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder showDialogHolder = null;
        if (null == convertView) {
            showDialogHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.company_search_item, null);
            showDialogHolder.name = (TextView) convertView.findViewById(R.id.title);
            convertView.setTag(showDialogHolder);
        } else {
            showDialogHolder = (ViewHolder) convertView.getTag();
        }

        final int index = position;

        if (null != mList.get(position).getCompanyname()) {
            SpannableString mm=  KeywordUtil.matcherSearchTitle(mContext.getResources().getColor(R.color.colorAccent),mList.get(position).getCompanyname(),keyword);

            showDialogHolder.name.setText(mm);
        }


        return convertView;
    }


    class ViewHolder {
        TextView name;
    }
}


